package com.itheima.bigevent.controller;

import com.itheima.bigevent.pojo.Article;
import com.itheima.bigevent.pojo.Category;
import com.itheima.bigevent.pojo.PageBean;
import com.itheima.bigevent.pojo.Result;
import com.itheima.bigevent.service.ArticleService;
import com.itheima.bigevent.service.impl.ArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: ArticleController
 * Package: com.itheima.bigevent.controller
 * Description:文章
 *
 * @Author: Zhang Peiyuan
 * @Create: 2024-08-19-16:36
 * @Version: v1.0
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService ariticleService;

    @PostMapping
    public Result<Integer> add(@RequestBody @Validated Article article){
        ariticleService.add(article);
        // 返回文章的id(主键)
        return Result.success(article.getId());
    }

    /**
     * 分页查询当前用户的文章
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param state
     * @return
     */
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum,
                                  Integer pageSize,
                                  @RequestParam(required = false) Integer categoryId,
                                  @RequestParam(required = false) String state){

        return Result.success(ariticleService.list(pageNum, pageSize, categoryId, state));

    }

    /**
     * 查询所有文章(不分用户)
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param state
     * @return
     */
    @GetMapping("/list/all")
    public Result<PageBean<Article>> listAll(Integer pageNum,
                                             Integer pageSize,
                                             @RequestParam(required = false) Integer categoryId,
                                             @RequestParam(required = false) String state){
        return Result.success(ariticleService.listAll(pageNum, pageSize, categoryId, state));
    }

    @GetMapping("detail/{id}")
    public Result<Article> detail(@PathVariable("id") Integer id){
        return Result.success(ariticleService.queryById(id));
    }

    @PutMapping()
    public Result update(@RequestBody Article article){
        ariticleService.update(article);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        ariticleService.delete(id);
        return Result.success();
    }
}
